package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.domain.CheckItem;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.CheckItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/checkitem")
public class CheckItemController {
    //引用注册中心 CheckItemService
    @Reference
    private CheckItemService checkItemService;

    /**
     * 添加检查项
     * @param checkItem
     * @return
     */
    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")//权限校验
    public Result add(@RequestBody CheckItem checkItem) {
        try {
            //调用service执行添加项
            checkItemService.add(checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            //执行失败
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
        //执行成功
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        //调用service的分页查询
        return checkItemService.findPage(queryPageBean);
    }

    /**
     * 根据id删除检查项
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")//权限校验
    public Result delete(Integer id) {
        try {
            //调用service的删除方法
            checkItemService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            //删除失败 返回错误信息
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        //删除成功 返回成功信息
        return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    /**
     * 根据id查询检查项信息
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")//权限校验
    public Result findById(Integer id) {
        try {
            //调用service的findById方法
            CheckItem checkItem = checkItemService.findById(id);
            //执行成功,将查询结果作为参数返回
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            //执行失败,返回错误信息
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    /**
     * 编辑检查项
     * @param checkItem
     * @return
     */
    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")//权限校验
    public Result edit(@RequestBody CheckItem checkItem) {
        try {
            //调用编辑方法
            checkItemService.edit(checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            //执行失败
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        //执行成功
        return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }

    /**
     * 查询所有
     * @return
     */
    @RequestMapping("/findAll")
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")//权限校验
    public Result findAll(){
        try {
            //调用findAll方法
            List<CheckItem> list = checkItemService.findAll();
            //执行成功,将list作为参数返回
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            //执行失败
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }

    }

}
